/**
 * Created by Administrator on 2018/3/29 0029.
 */
import org.omg.CORBA.*;

import java.io.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;

public class p298 {
    public static void main(String[] args) {
        String command="渡江总攻时间是4月22日晚10点";
        EncryptAndDecrypt person=new EncryptAndDecrypt();
        String passward="Tiger";
        String secret=person.encrypt(command,passward);
        File file=new File("secret.txt");
        try{
            FileOutputStream fos=new FileOutputStream(file);
            DataOutputStream outData=new DataOutputStream(fos);
            outData.writeUTF(secret);
            System.out.println("加密命令:"+secret);
        }
        catch (IOException e){}
        try{
            FileInputStream fis=new FileInputStream(file);
            DataInputStream inData=new DataInputStream(fis);
            String str=inData.readUTF();
            String mingwen=person.decrypt(str,passward);
            System.out.println("解密命令"+mingwen);
        }
        catch (IOException e){}
    }
}
class EncryptAndDecrypt {
    String encrypt(String sourceString,String password){
        char[] p= password.toCharArray();
        int n=p.length;
        char [] c=sourceString.toCharArray();
        int m=c.length;
        for(int k=0;k<m;k++){
            int mima=c[k]+p[k%n];
            c[k]=(char)mima;
        }
        return new String(c);
    }
    String decrypt(String sourceString,String password) {
        char [] p=password.toCharArray();
        int n=p.length;
        char []c=sourceString.toCharArray();
        int m=c.length;
        for(int k=0;k<m;k++) {
            int mima=c[k]-p[k%n];
            c[k]=(char)mima;
        }
        return  new String(c);
    }

}
